package com.micromsg.common.config;

import com.micromsg.common.core.ApiResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

@Slf4j
@ControllerAdvice
public class GlobalExceptionHandler {

//    @ExceptionHandler(Exception.class)
//    public String handleException(Exception e, Model model) {
//        // 处理异常，并将异常信息放入模型
//        model.addAttribute("errorMessage", e.getMessage());
//        // 返回异常视图名称
//        return "error";
//    }

    @ExceptionHandler(Exception.class)
    public ResponseEntity<ApiResult<Object>> handleException(Exception e) {
        // 处理异常，并返回包含错误信息的对象
        ResponseEntity<ApiResult<Object>> entity = ApiResult.error(e);
        log.error("全局异常信息:{}", e.getMessage(), e);
        return entity;
    }

//    @ExceptionHandler(Exception.class)
//    public ResponseEntity<String> handleException(Exception e) {
//        // 在这里处理异常，并返回适当的响应
//        return new ResponseEntity<>("An error occurred: " + e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
//    }

//    @ExceptionHandler(RuntimeException.class)
//    public ResponseEntity<String> handleRuntimeException(RuntimeException e) {
//        // 处理RuntimeException
//        return new ResponseEntity<>("Runtime error occurred: " + e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
//    }

//    @ExceptionHandler(MyCustomException.class)
//    public ResponseEntity<String> handleMyCustomException(MyCustomException e) {
//        // 处理自定义异常
//        return new ResponseEntity<>("Custom error occurred: " + e.getMessage(), HttpStatus.BAD_REQUEST);
//    }

}
